<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Notices;
use DB;
use Illuminate\Http\Request;

class NoticeController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return $this->success( Notices::paginate(10));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $res=Notices::create($request->all());
        $user=DB::table('users')->get()->map(function($value){
            return (array) $value;
        });

        foreach($user as $item){
            $u['users_id']=$item['id'];
            $u['notice_id']=$res->id;
            $u['created_at']=time();
            $notice[]=$u;
        }
        
        
         if( DB::table('notices_users')->insert($notice))  return $this->success();
         return $this->apiError();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if( Notices::where('id',$id)->update($request->all()))  return $this->success();
         return $this->apiError();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(Notices::destroy($id)) return $this->success();
        return $this->apiError();
    }
}
